//
//  SearchViewController.m
//  OverseasOnlineShopping
//
//  Created by 唐先才 on 16/3/7.
//  Copyright © 2016年 唐先才. All rights reserved.
//

#import "SearchViewController.h"
#import "TxcPrefixHeader.pch"
#import "OverseasOnlineShopping-Swift.h"

@interface SearchViewController ()
{
    UIView *_barView;
    UIButton *_button;
    UIPageViewController *_pvc;
    UIViewController * _willVC;
    NSArray *_viewControllersArray;
}
@end

@implementation SearchViewController

- (void)viewDidLoad {
    self.view.backgroundColor = [UIColor whiteColor];
    [self createNavBar];
    [self createPageViewController];
}

- (void)createNavBar {
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(60, 0, SCREEN_WIDTH - 60 - 100, 25)];
    searchBar.placeholder = @"搜一搜";
    UIView *searchView = [[searchBar subviews] lastObject];
    UITextField *searchField = [[searchView subviews] lastObject];
    searchField.font = [UIFont systemFontOfSize:11];
    searchField.textColor = [UIColor grayColor];
    searchBar.layer.cornerRadius = 5;
    searchBar.clipsToBounds = YES;
    searchBar.backgroundColor = [UIColor whiteColor];
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    [titleView addSubview:searchBar];
    self.navigationItem.titleView = titleView;
}

- (void)createPageViewController {
    
    NSArray *titleArray = @[@"分类", @"商城"];
    for (NSInteger i = 0; i < 2; i ++) {
        _button = [UIButton buttonWithType:UIButtonTypeSystem];
        _button.frame = CGRectMake(i * SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, 28);
        [_button setTitle:titleArray[i] forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        if (i==0) {
            [_button setTitleColor:[UIColor colorWithRed:219 / 255.0 green:35 / 255.0 blue:47 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        } else {
            [_button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
        _button.titleLabel.textAlignment = NSTextAlignmentCenter;
        _button.titleLabel.font = [UIFont systemFontOfSize:15];
        _button.tag = 100 + i ;
        [_button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_button];
    }
    UIButton *button = (UIButton *)[self.view viewWithTag:100];
    _button=button;
    
    _barView = [[UIView alloc] init];
    _barView.frame = CGRectMake(SCREEN_WIDTH / 8, 28, SCREEN_WIDTH / 4, 2);
    _barView.backgroundColor = [UIColor colorWithRed:219 / 255.0 green:35 / 255.0 blue:47 / 255.0 alpha:1.0];
    [self.view addSubview:_barView];
    
    _pvc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    _pvc.view.backgroundColor = [UIColor whiteColor];
    _pvc.view.frame = CGRectMake(0, 30, SCREEN_WIDTH, SCREEN_HEIGHT - 30);
    _pvc.dataSource = self;
    _pvc.delegate = self;
    [self.view addSubview:_pvc.view];
    
    CategoryViewController *categoryVC = [[CategoryViewController alloc] init];
    categoryVC.viewController = self;
    categoryVC.view.backgroundColor = [UIColor whiteColor];
    MallViewController *mall = [[MallViewController alloc] init];
    _viewControllersArray = @[categoryVC, mall];
    [_pvc setViewControllers:@[categoryVC] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
}

- (void)clickAction:(UIButton *)button {
    [_button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    if (button.tag == 101) {
        [UIView animateWithDuration:0.3 animations:^{
            _barView.frame = CGRectMake(5 * SCREEN_WIDTH / 8, 28, SCREEN_WIDTH / 4, 2);
        }];
        [button setTitleColor:[UIColor colorWithRed:219 / 255.0 green:35 / 255.0 blue:47 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        MallViewController *mall = [[MallViewController alloc] init];
        [_pvc setViewControllers:@[mall] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
    } else if (button.tag == 100) {
        [UIView animateWithDuration:0.3 animations:^{
            _barView.frame = CGRectMake(SCREEN_WIDTH / 8, 28, SCREEN_WIDTH / 4, 2);
        }];
        [button setTitleColor:[UIColor colorWithRed:219 / 255.0 green:35 / 255.0 blue:47 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        CategoryViewController *categoryVC = [[CategoryViewController alloc] init];
        [_pvc setViewControllers:@[categoryVC] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
    }
    
    _button = button;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if ([viewController isKindOfClass:[MallViewController class]]) {
        return nil;
    }
    return _viewControllersArray[1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if ([viewController isKindOfClass:[CategoryViewController class]]) {
        return nil;
    }
    return _viewControllersArray[0];
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    
    if (completed) {
        if ([_willVC isKindOfClass:[MallViewController class]]) {
            [_button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [UIView animateWithDuration:0.3 animations:^{
                _barView.frame = CGRectMake(5 * SCREEN_WIDTH / 8, 28, SCREEN_WIDTH / 4, 2);
            }];
            UIButton *button = (UIButton *)[self.view viewWithTag:101];
            [button setTitleColor:[UIColor colorWithRed:219 / 255.0 green:35 / 255.0 blue:47 / 255.0 alpha:1.0] forState:UIControlStateNormal];
            _button=button;
        } else {
            [_button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [UIView animateWithDuration:0.3 animations:^{
                _barView.frame = CGRectMake(SCREEN_WIDTH / 8, 28, SCREEN_WIDTH / 4, 2);
            }];
            UIButton *button = (UIButton *)[self.view viewWithTag:100];
            [button setTitleColor:[UIColor colorWithRed:219 / 255.0 green:35 / 255.0 blue:47 / 255.0 alpha:1.0] forState:UIControlStateNormal];
            _button=button;
        }
    }
}

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers{
    _willVC = pendingViewControllers[0];
}


@end
